﻿using System;
using System.Collections.Generic;
using System.Text;
using NExt.tree;
using NExt.Core.Model.Base;

namespace NExt.Demo.Application
{
    public class EntityListPresenter : TreePanel
    {
        public EntityListPresenter()
        {
            width = 200;
            title = "Entity List";
            animate = true;
            enableDD = true;
            root = BuildNodes();
            extended = new { region = "west", collapsible = true };
            loader = new TreeLoader() { dataUrl = "next/core/NExt.Core.dll" };
        }

        private object BuildNodes()
        {
            AsyncTreeNode root = new AsyncTreeNode() { text = "Root", expanded = true, draggable = false };

            //AddEntity<Entity1.Browse>(root, "Entity1");
            AddEntity<Entity.PresenterBase.Browse>(root, "Entity.PresenterBase");
            AddEntity<Entity2.Browse>(root, "Entity2");
            AddEntity<Entity.Bind.Browse>(root, "Entity Bind");
            AddEntity<Entity3.Browse>(root, "Entity Bind Complex Type");
            return root;
        }

        private static void AddEntity<TEntity>(AsyncTreeNode root, string name)
        {
            string typeName = typeof(TEntity).FullName;
            string runScript = string.Format("var presenter = new {0}(); presenter.title = '{0}'; presenter.fireShow();", typeName);
            AsyncTreeNode child = new AsyncTreeNode() { text = name, draggable = false, leaf = true, cls = "file" };
            child.listeners.dblclick = new AnonymousFunction(runScript);
            //child.listeners.dblclick = new Script("Temp");
            root.children.Add(child);
        }
    }
}
